package com.eddie.mediator;

import java.util.HashMap;

/**
 * 具体的中介者对象类
 *
 * @author Eddie
 * @date 2022/08/04 11:00
 **/
public class ConcreteMediator extends Mediator {

    /**
     * 集合，放入了所有同事对象
     */
    private HashMap<String, College> collegeMap;


    private HashMap<String, String> interMap;

    /**
     * 构造器将集合初始化
     */
    public ConcreteMediator() {
        collegeMap = new HashMap<>();
        interMap = new HashMap<>();
    }

    @Override
    public void register(String colleagueName, College college) {
        collegeMap.put(colleagueName, college);

        if (college instanceof Alarm) {
            interMap.put("Alarm", colleagueName);
        } else if (college instanceof TV) {
            interMap.put("TV", colleagueName);
        } else if (college instanceof CoffeeMachine) {
            interMap.put("Coffee", colleagueName);
        } else if (college instanceof Curtains) {
            interMap.put("Curtains", colleagueName);
        }
    }

    /**
     * 具体的核心方法
     * 1、根据得到的消息，完成对应的任务
     * 2、中介者在这个方法中，协调各个具体的同事对象，完成任务
     *
     * @param stateChange   状态变化
     * @param colleagueName 同时名字
     */
    @Override
    public void getMessage(int stateChange, String colleagueName) {
        if (collegeMap.get(colleagueName) instanceof Alarm) {
            if (stateChange == 0) {
                ((CoffeeMachine) collegeMap.get(interMap.get("Coffee"))).start();
                ((TV) collegeMap.get(interMap.get("TV"))).start();
            } else if (stateChange == 1) {
                ((TV) collegeMap.get(interMap.get("TV"))).stop();
            }
        } else if (collegeMap.get(colleagueName) instanceof CoffeeMachine) {
            ((Curtains) collegeMap.get(interMap.get("Curtains"))).up();
        } else if (collegeMap.get(colleagueName) instanceof TV) {//如果TV发出的消息这里处理

        } else if (collegeMap.get(colleagueName) instanceof Curtains) {//如果是窗帘发出的消息这里处理

        }
    }

    @Override
    public void sendMessage() {

    }
}
